package com.arpa.wms.cancel.delivery;

import cn.hutool.json.JSONUtil;
import com.arpa.wms.cancel.DeliveryCancelHandlerTypeConstants;
import com.arpa.wms.domain.entity.Delivery;
import com.arpa.wms.domain.enums.DeliveryStatusEnum;
import com.arpa.wms.service.IDeliveryService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import static com.arpa.wms.domain.consts.NumConst.NUM_ONE;

/**
 * 新增订单取消处理
 * 直接更新状态为作废
 *
 * @author leo
 */
@Log4j2
@Component(DeliveryCancelHandlerTypeConstants.ADD)
public class AddDeliveryCancelHandler extends AbstractDeliveryCancelHandler {

    /**
     * 已分配订单取消处理
     * 作废；已分配库存退回原库位；
     * @author liuxr
     * @date 2021/6/10 8:58
     * @param deliveryService
     * @param delivery
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean handle(IDeliveryService deliveryService, Delivery delivery) {
        log.info("新增、待分配订单取消处理。订单信息：{}", JSONUtil.toJsonStr(delivery));
        log.info("新增、待分配订单取消处理【step1】：更新订单作废");
        // 更新订单的状态，改为作废
        boolean bool = deliveryService.update(Wrappers.lambdaUpdate(Delivery.class)
                .eq(Delivery::getCode, delivery.getCode())
                .eq(Delivery::getIsCancel, NUM_ONE)
                .set(Delivery::getStatus, DeliveryStatusEnum.VOID.getValue()));
        log.info("新增、待分配订单取消处理,订单【{}】状态为：{}，直接更新状态为作废,更新结果：{}", delivery.getCode(), delivery.getStatus(), bool);
        return bool;
    }
}
